package uk.co.q3c.deplan.client.ui.view;

import java.util.ArrayList;

import uk.co.q3c.deplan.client.GreetingService;
import uk.co.q3c.deplan.client.GreetingServiceAsync;
import uk.co.q3c.deplan.client.ui.component.HorizontalFlowPanel;
import uk.co.q3c.deplan.client.ui.component.VerticalFlowPanel;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HorizontalSplitPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.StackLayoutPanel;
import com.google.gwt.user.client.ui.Widget;

public class ScreenViewGWT implements ScreenView {

	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting
	 * service.
	 */
	private final GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
	private StackLayoutPanel stackPanel;
	// private VerticalPanel selectorPanel = new VerticalPanel();
	private ArrayList<String> reminders;
	private ListBox reminderList;
	private HorizontalSplitPanel mainPanel;
	private HorizontalFlowPanel breadcrumb;
	private FlowPanel messageBar;
	int borderWidth = 1;
	final int buttonWidth = 70;
	private DockLayoutPanel bodyPanel;

	private VerticalFlowPanel navPanel;
	private MenuBar menuBar;
	private MenuItem mntmResources;
	private MenuItem mntmProjects;
	private MenuItem mntmHome;
	private ScrollPanel corePanelArea;
	private final ScreenPresenter presenter;

	public ScreenViewGWT() {
		super();
		this.presenter = new ScreenPresenter(this);
		History.addValueChangeHandler(presenter);
		construct();
		layout();
		RootLayoutPanel.get()
				.add(mainPanel);
		presenter.prepareMenus();
	}

	private void construct() {

		mainPanel = new HorizontalSplitPanel();
		navPanel = new VerticalFlowPanel();
		bodyPanel = new DockLayoutPanel(Unit.PX);
		stackPanel = new StackLayoutPanel(Unit.PX);
		reminderList = new ListBox();

		corePanelArea = new ScrollPanel();
		// just for testing
		reminders = new ArrayList<String>();
		reminders.add("Reminder 1");
		reminders.add("Reminder 2");
		reminders.add("Reminder 3");

		mntmResources = new MenuItem("Resources", false, cmdNotSet);
		mntmProjects = new MenuItem("Projects", false, cmdNotSet);
		mntmHome = new MenuItem("Home", false, cmdNotSet);
	}

	/**
	 * A default command used in initial construction of menu items. It just
	 * pops up a message to say that a command has not been set - this would not
	 * happen if the proper commands have been set using, for example,
	 * {@link #setProjectsMICommand(Command)}
	 */
	public Command cmdNotSet = new Command() {
		public void execute() {
			Window.alert("Command has not been set");
		}
	};

	private void layout() {
		mainPanel.setLeftWidget(navPanel);
		mainPanel.setRightWidget(bodyPanel);

		bodyPanel.setSize("100%", "100%");
		bodyPanel.setStyleName("mainScreen");
		// bodyPanel.addStyleName("panelFinder");

		navPanel.add(stackPanel());
		reminderList.setVisibleItemCount(8);
		reminderList.setWidth("100%");

		bodyPanel.addSouth(messageBar(), 20);

		// bodyPanel.addNorth(topBar(), 30);

		menuBar = new MenuBar(false);
		bodyPanel.addNorth(menuBar, 30);
		menuBar.setSize("100%", "20");

		menuBar.addItem(mntmHome);
		menuBar.addItem(mntmProjects);
		menuBar.addItem(mntmResources);
		;
		bodyPanel.add(corePanelArea);
		// corePanelArea.setSize("100%", "100%");
		mainPanel.setSplitPosition("200px");
	}

	/**
	 * @return
	 */
	private Widget messageBar() {
		messageBar = new HorizontalFlowPanel();
		messageBar.setWidth("100%");
		breadcrumb = new HorizontalFlowPanel();
		messageBar.add(breadcrumb);
		// breadcrumb.setHeight("40px");
		Hyperlink link1 = new Hyperlink();
		link1.setText("deplan");
		breadcrumb.add(link1);
		Hyperlink link2 = new Hyperlink();
		link2.setText("design");
		breadcrumb.add(new Label(" > "));
		breadcrumb.add(link2);
		messageBar.add(new Label("message and status info"));
		messageBar.add(new Label("  .... and more info"));
		return messageBar;
	}

	private Widget breadcrumb() {

		breadcrumb.setWidth("100%");
		breadcrumb.setHeight("100%");
		// breadcrumb.setBorderWidth(borderWidth);
		// breadcrumb.setHorizontalAlignment(HasAlignment.ALIGN_LEFT);

		return breadcrumb;
	}

	/**
	 * @return
	 */
	private Widget stackPanel() {
		stackPanel.setHeight("150px");
		stackPanel.setWidth("100%");
		stackPanel.add(reminderList, new Button("Reminders"), 20);

		stackPanel.add(new Label("Other stuff"), new Button("Other"), 20);
		return stackPanel;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * uk.co.q3c.deplan.client.ui.view.ScreenView#setCorePanel(uk.co.q3c.deplan
	 * .client.ui.Q3View)
	 */
	public void setCorePanel(Q3CoreView newView) {
		Widget w = corePanelArea.getWidget();
		if (w != null) {
			corePanelArea.remove(w);
		}
		Widget nv = newView.basePanel();
		nv.setStyleName("panels");
		nv.setSize("100%", "100%");
		corePanelArea.add(nv);

	}

	@Override
	public void setHomeMICommand(Command cmd) {
		mntmHome.setCommand(cmd);

	}

	@Override
	public void setProjectsMICommand(Command cmd) {
		mntmProjects.setCommand(cmd);

	}

	@Override
	public void setResourcesMICommand(Command cmd) {
		mntmResources.setCommand(cmd);

	}

	public ScreenPresenter getPresenter() {
		return presenter;
	}

}
